// pages/pingjia/pingjia.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sourcesData:[],
    evaluate_order:[],
    imgs: [{
      id: 1
    }, {
      id: 2
    }, {
      id: 3
    }, {
      id: 4
    }, {
      id: 5
      }],
    // starId: 5,
    starId1: 5,
    starId2: 5,
    starId3: 5,
    src1: '/image/star.png',
    src2: '/image/grayStar.png',
    order_id:'',
    pingjext:'',
    ind:0,
    addind_arr:[]
  },
  select1(e) {
    console.log(e)
    this.data.starId1 = e.currentTarget.dataset.index;
    this.setData({
      starId1: this.data.starId1
    })
  },
  select2(e) {
    console.log(e)
    this.data.starId2 = e.currentTarget.dataset.index;
    this.setData({
      starId2: this.data.starId2
    })
  },
  select3(e) {
    console.log(e)
    this.data.starId3 = e.currentTarget.dataset.index;
    this.setData({
      starId3: this.data.starId3
    })
  },
  uploadimg: function () {
    var that = this;
    wx.chooseImage({  //从本地相册选择图片或使用相机拍照
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片，批量添加的话，就是一个图片地址数组
        var tempFilePaths = res.tempFilePaths
        wx.setStorageSync('tempFilePaths', res.tempFilePaths)
        that.setData({
          sourcess: res.tempFilePaths  //这个用于展示添加的图片，并非服务器图片地址
        })

        //添加图片后，会弹窗提示，确认是否上传
        wx.showModal({
          title: '提示',
          content: '确认上传这些吗',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              var tempFilePaths = wx.getStorageSync('tempFilePaths')
              var sources = [];
              //循环将添加的图片上传到服务器
              for (var i = 0; i < tempFilePaths.length; i++) {
                wx.showToast({
                  title: '正在上传中',
                  icon: 'loading',
                  duration: 15000
                })
                wx.uploadFile({
                  url: ApiUrl+'/imgupload.php?imgpath=userimg',
                  filePath: tempFilePaths[i],
                  name: 'file',
                  success: function (res) {
                    wx.hideToast();
                    console.log(typeof res.data)
                    if (res.data !== "上传错误") {
                      sources.push(JSON.parse(res.data));
                      //前台显示
                      that.setData({
                        sourcesData: sources
                      })
                      console.log(sources)
                    }
                  }
                })
              }
            } else if (res.cancel) {
              console.log('用户点击取消')
              that.setData({
                sourcess: "",
              })
            }
          }
        })
      }
    })
  },
  pingjiatext(e){
    let txt = e.detail.value;
    let i = this.data.ind;
    let evaluate_order = this.data.evaluate_order;
    console.log(txt);
    evaluate_order[i].pinglun = txt;
    console.log(evaluate_order);
    this.setData({
      evaluate_order: evaluate_order
    })
  }, 
  select(e) {
    console.log(e)
    let score = e.currentTarget.dataset.index;
    console.log(score)
    let i = this.data.ind;
    let evaluate_order = this.data.evaluate_order;
    evaluate_order[i].score = score;
    console.log(evaluate_order);
    this.setData({
      evaluate_order: evaluate_order
    })
  },
  //评价商品
  //index.php?act=member_evaluate&op=index
  evaluate_order(id) {
    var that = this;
    let k = wx.getStorageSync('key');
    var url = 'index.php?act=member_evaluate&op=index&order_id='+id;
    let list = { key: k}
    console.log('我的评价参数：===》',list);
    mph.postRequest(url, list, function (res) {
      if (res.data.code == 200) {
        console.log('我的评价:', res.data.datas);
        let arr = res.data.datas.order_goods;

        for(let i=0;i<arr.length;i++){
            arr[i].pinglun = '';
            arr[i].score = '5';
            arr[i].anony = 0;
            arr[i].evaluate_image = [];
        }

        that.setData({
          evaluate_order: arr
        })
      }
    });
  },
  add_ind(e){
    console.log(e.currentTarget.dataset.ind);
    this.setData({
      ind: e.currentTarget.dataset.ind
    })
  },
  //提交保存评价信息
  //index.php?act=member_evaluate&op=save
  member_evaluate() {
    var that = this; 
    let k = wx.getStorageSync('key');
    let evaluate_order = this.data.evaluate_order;
    let arr = [];
    for (let i = 0; i < evaluate_order.length;i++){
      let json = {
        score: evaluate_order[i].score,//商品评分
        comment: evaluate_order[i].pinglun,//评论内容，不超过150字
        anony: evaluate_order[i].anony,//是否匿名
        evaluate_image: evaluate_order[i].evaluate_image
      }
      arr.push(json);
    }
    var url = 'index.php?act=member_evaluate&op=save';
    let list = {
      key: k,
      order_id: that.data.order_id,
      goods: arr,
      store_desccredit: that.data.starId1,
      store_servicecredit: that.data.starId2,
      store_deliverycredit: that.data.starId3
     }
    console.log('我的评价参数：===》', list);
    //需要传对象只能使用json传值，
    wx.request({
      url: ApiUrl + '/index.php?act=member_evaluate&op=save',
      data: list,
      method: 'POST',
      dataType: 'json',
      success: function (res) {
      if (res.data.code == 200) {
        console.log('我的评价:', res.data.datas);
        wx.redirectTo({
          url: '/pages/my_order/my_order?name=state_noeval'
        })
      }
    }});
  },
  tijiaobtn(){
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('评价得牧草：====》',options);
    if (options.order_id){
      this.evaluate_order(options.order_id);
      this.setData({
        order_id: options.order_id
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})